/**
 * 
 */
package com.googlecode.gauth.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.gauth.beans.User;
import com.googlecode.gauth.factory.SiteRegistryFactory;
import com.googlecode.gauth.factory.UserServiceFactory;
import com.googlecode.gauth.service.SiteRegister;
import com.googlecode.gauth.service.UserService;
import com.googlecode.gauth.util.GAuthUtil;

/**
 * @author asanthan
 * 
 */
public class UserServiceImpl implements UserService {

	private SiteRegister siteRegister = SiteRegistryFactory.createSiteRegister();

	public UserServiceImpl() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.gauth.service.UserService#isLoggedIn(javax.servlet.http
	 * .HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public boolean isLoggedIn(HttpServletRequest pRequest, HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ? siteUserService.isLoggedIn(pRequest, pResponse) : false;

	}

	private UserService getUserServiceBySiteId(Long pSiteId) {
		if (pSiteId != null) {
			return siteRegister.getSiteUserService(pSiteId);
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.gauth.service.UserService#isAdmin(javax.servlet.http.
	 * HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public boolean isAdmin(HttpServletRequest pRequest, HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ? UserServiceFactory.isAdmin(siteUserService
				.getLoggedInUser(pRequest, pResponse)) : false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.gauth.service.UserService#getLoggedInUser(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public User getLoggedInUser(HttpServletRequest pRequest, HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ? siteUserService.getLoggedInUser(pRequest, pResponse)
				: null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.gauth.service.UserService#getLoginInUrl(javax.servlet.
	 * http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public String getLoginInUrl(HttpServletRequest pRequest, HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ? siteUserService.getLoginInUrl(pRequest, pResponse) : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.gauth.service.UserService#getLogOutUrl(javax.servlet.http
	 * .HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public String getLogOutUrl(HttpServletRequest pRequest, HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ? siteUserService.getLogOutUrl(pRequest, pResponse) : null;
	}

	@Override
	public boolean processCallBack(HttpServletRequest pRequest,
			HttpServletResponse pResponse) {
		Long siteId = GAuthUtil.getSiteIdFromCookie(pRequest);
		UserService siteUserService = getUserServiceBySiteId(siteId);
		return siteUserService != null ?siteUserService.processCallBack(pRequest, pResponse):false;
	}

}
